library(dplyr)
library(ggplot2)
library(tidyr)
library(ggplot2)
library(reshape2)
library(data.table)
library(broom)
library(olsrr)
library(corrplot)
library(plotly)

Import data

setwd("D:/Project")
air <- read.csv("Air.csv", sep=";", dec=",")
air <- air[,-c(16, 17)]

Make it long and check data. We can see outliers in -200 - replace it with NA. We can see that one of columns - Non Metanic HydroCarbons concentration have a lot of NAs. Let’s drop it and then drop NAs. We saved data loosing one column. Also we can notice that not all columns have normal distribution

str(air)
## 'data.frame':    9471 obs. of  15 variables:
##  $ Date         : Factor w/ 392 levels "","01/01/2005",..: 116 116 116 116 116 116 129 129 129 129 ...
##  $ Time         : Factor w/ 25 levels "","00.00.00",..: 20 21 22 23 24 25 2 3 4 5 ...
##  $ CO.GT.       : num  2.6 2 2.2 2.2 1.6 1.2 1.2 1 0.9 0.6 ...
##  $ PT08.S1.CO.  : int  1360 1292 1402 1376 1272 1197 1185 1136 1094 1010 ...
##  $ NMHC.GT.     : int  150 112 88 80 51 38 31 31 24 19 ...
##  $ C6H6.GT.     : num  11.9 9.4 9 9.2 6.5 4.7 3.6 3.3 2.3 1.7 ...
##  $ PT08.S2.NMHC.: int  1046 955 939 948 836 750 690 672 609 561 ...
##  $ NOx.GT.      : int  166 103 131 172 131 89 62 62 45 -200 ...
##  $ PT08.S3.NOx. : int  1056 1174 1140 1092 1205 1337 1462 1453 1579 1705 ...
##  $ NO2.GT.      : int  113 92 114 122 116 96 77 76 60 -200 ...
##  $ PT08.S4.NO2. : int  1692 1559 1555 1584 1490 1393 1333 1333 1276 1235 ...
##  $ PT08.S5.O3.  : int  1268 972 1074 1203 1110 949 733 730 620 501 ...
##  $ T            : num  13.6 13.3 11.9 11 11.2 11.2 11.3 10.7 10.7 10.3 ...
##  $ RH           : num  48.9 47.7 54 60 59.6 59.2 56.8 60 59.7 60.2 ...
##  $ AH           : num  0.758 0.726 0.75 0.787 0.789 ...
for (i in 3:ncol(air)) {
  air[,i][which(air[,i] == -200)] <- NA
}
air_long <- melt(air)
## Warning in melt(air): The melt generic in data.table has been passed a
## data.frame and will attempt to redirect to the relevant reshape2 method;
## please note that reshape2 is deprecated, and this redirection is now
## deprecated as well. To continue using melt methods from reshape2 while both
## libraries are attached, e.g. melt.list, you can prepend the namespace like
## reshape2::melt(air). In the next version, this warning will become an error.
air_long <- air_long[,-c(1,2)]
ggplot(air_long, aes(value)) + 
  geom_histogram() + 
  facet_wrap(~variable, scales = "free")
## Warning: Removed 18183 rows containing non-finite values (stat_bin).

air <- air[,c(1:4,6:15)]
air <- drop_na(air)
air_long <- melt(air)
## Warning in melt(air): The melt generic in data.table has been passed a
## data.frame and will attempt to redirect to the relevant reshape2 method;
## please note that reshape2 is deprecated, and this redirection is now
## deprecated as well. To continue using melt methods from reshape2 while both
## libraries are attached, e.g. melt.list, you can prepend the namespace like
## reshape2::melt(air). In the next version, this warning will become an error.
air_long <- air_long[,-c(1,2)]
summary(air)
##          Date            Time          CO.GT.        PT08.S1.CO.  
##  02/04/2005:  24   10.00.00: 312   Min.   : 0.100   Min.   : 647  
##  03/04/2005:  24   20.00.00: 310   1st Qu.: 1.100   1st Qu.: 956  
##  15/03/2005:  24   09.00.00: 309   Median : 1.900   Median :1085  
##  16/03/2005:  24   12.00.00: 309   Mean   : 2.182   Mean   :1120  
##  18/03/2005:  24   18.00.00: 309   3rd Qu.: 2.900   3rd Qu.:1254  
##  19/03/2005:  24   21.00.00: 309   Max.   :11.900   Max.   :2040  
##  (Other)   :6797   (Other) :5083                                  
##     C6H6.GT.     PT08.S2.NMHC.       NOx.GT.        PT08.S3.NOx.   
##  Min.   : 0.20   Min.   : 390.0   Min.   :   2.0   Min.   : 322.0  
##  1st Qu.: 4.90   1st Qu.: 760.0   1st Qu.: 103.0   1st Qu.: 642.0  
##  Median : 8.80   Median : 931.0   Median : 186.0   Median : 786.0  
##  Mean   :10.55   Mean   : 958.5   Mean   : 250.7   Mean   : 816.9  
##  3rd Qu.:14.60   3rd Qu.:1135.0   3rd Qu.: 335.0   3rd Qu.: 947.0  
##  Max.   :63.70   Max.   :2214.0   Max.   :1479.0   Max.   :2683.0  
##                                                                    
##     NO2.GT.       PT08.S4.NO2.   PT08.S5.O3.         T               RH       
##  Min.   :  2.0   Min.   : 551   Min.   : 221   Min.   :-1.90   Min.   : 9.20  
##  1st Qu.: 79.0   1st Qu.:1207   1st Qu.: 760   1st Qu.:11.20   1st Qu.:35.30  
##  Median :110.0   Median :1457   Median :1006   Median :16.80   Median :49.20  
##  Mean   :113.9   Mean   :1453   Mean   :1058   Mean   :17.76   Mean   :48.88  
##  3rd Qu.:142.0   3rd Qu.:1683   3rd Qu.:1322   3rd Qu.:23.70   3rd Qu.:62.20  
##  Max.   :333.0   Max.   :2775   Max.   :2523   Max.   :44.60   Max.   :88.70  
##                                                                               
##        AH        
##  Min.   :0.1847  
##  1st Qu.:0.6941  
##  Median :0.9539  
##  Mean   :0.9856  
##  3rd Qu.:1.2516  
##  Max.   :2.1806  
## 

So, we need to normolize our data.

normalize <- function(x) {
return ((x - min(x)) / (max(x) - min(x)))
}
air_norm <- as.data.frame(lapply(air[3:14], normalize))
air_long <- melt(air_norm)
## Warning in melt(air_norm): The melt generic in data.table has been passed
## a data.frame and will attempt to redirect to the relevant reshape2 method;
## please note that reshape2 is deprecated, and this redirection is now
## deprecated as well. To continue using melt methods from reshape2 while both
## libraries are attached, e.g. melt.list, you can prepend the namespace like
## reshape2::melt(air_norm). In the next version, this warning will become an
## error.
## No id variables; using all as measure variables

Check our data. Quantile - the proportion of cases that are less than certain values. If the requirements of “normality” are here - the data should lie on a diagonal line. We can see that several columns do not have normal distribution.

#CO.GT
qqnorm(air_norm$CO.GT., pch = 1, frame = FALSE)
qqline(air_norm$CO.GT., col = "steelblue", lwd = 2)

#PT08.S1.CO
qqnorm(air_norm$PT08.S1.CO., pch = 1, frame = FALSE)
qqline(air_norm$PT08.S1.CO., col = "steelblue", lwd = 2)

#C6H6.GT
qqnorm(air_norm$C6H6.GT., pch = 1, frame = FALSE)
qqline(air_norm$C6H6.GT., col = "steelblue", lwd = 2)

#PT08.S2.NMHC
qqnorm(air_norm$PT08.S2.NMHC., pch = 1, frame = FALSE)
qqline(air_norm$PT08.S2.NMHC., col = "steelblue", lwd = 2)

#NOx.GT
qqnorm(air_norm$NOx.GT., pch = 1, frame = FALSE)
qqline(air_norm$NOx.GT., col = "steelblue", lwd = 2)

#PT08.S3.NOx
qqnorm(air_norm$PT08.S3.NOx., pch = 1, frame = FALSE)
qqline(air_norm$PT08.S3.NOx., col = "steelblue", lwd = 2)

#NO2.GT
qqnorm(air_norm$NO2.GT., pch = 1, frame = FALSE)
qqline(air_norm$NO2.GT., col = "steelblue", lwd = 2)

#PT08.S4.NO2
qqnorm(air_norm$PT08.S4.NO2., pch = 1, frame = FALSE)
qqline(air_norm$PT08.S4.NO2., col = "steelblue", lwd = 2)

#PT08.S5.O3
qqnorm(air_norm$PT08.S5.O3., pch = 1, frame = FALSE)
qqline(air_norm$PT08.S5.O3., col = "steelblue", lwd = 2)

#T
qqnorm(air_norm$T, pch = 1, frame = FALSE)
qqline(air_norm$T, col = "steelblue", lwd = 2)

#RH
qqnorm(air_norm$RH, pch = 1, frame = FALSE)
qqline(air_norm$RH, col = "steelblue", lwd = 2)

#AH
qqnorm(air_norm$AH, pch = 1, frame = FALSE)
qqline(air_norm$AH, col = "steelblue", lwd = 2)

Response C6H6.GT. We can see columns that have linear response to C6H6.GT.

mod <- lm(data = air_norm, C6H6.GT. ~ CO.GT.)
summary(mod)
## 
## Call:
## lm(formula = C6H6.GT. ~ CO.GT., data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.23935 -0.02496 -0.00249  0.02357  0.76733 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.0050753  0.0009115   5.568 2.67e-08 ***
## CO.GT.      0.8952134  0.0042471 210.782  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.04321 on 6939 degrees of freedom
## Multiple R-squared:  0.8649, Adjusted R-squared:  0.8649 
## F-statistic: 4.443e+04 on 1 and 6939 DF,  p-value: < 2.2e-16
augment(mod)%>%
   ggplot(aes(x = CO.GT., y = C6H6.GT.))+
  geom_point()+
  geom_line(aes(y= .fitted), color = "blue", size = 1)

residuals(mod) %>% hist(main = "Residuals CO.GT.")

plot(mod, which = 1)

mod1 <- lm(data = air_norm, C6H6.GT. ~ PT08.S1.CO.)
summary(mod1)
## 
## Call:
## lm(formula = C6H6.GT. ~ PT08.S1.CO., data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.18826 -0.03559 -0.00288  0.03079  0.70768 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.059959   0.001613  -37.18   <2e-16 ***
## PT08.S1.CO.  0.656927   0.004312  152.37   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.0564 on 6939 degrees of freedom
## Multiple R-squared:  0.7699, Adjusted R-squared:  0.7699 
## F-statistic: 2.322e+04 on 1 and 6939 DF,  p-value: < 2.2e-16
augment(mod1)%>%
   ggplot(aes(x = PT08.S1.CO., y = C6H6.GT.))+
  geom_point()+
  geom_line(aes(y= .fitted), color = "blue", size = 1)

residuals(mod1) %>% hist(main = "Residuals PT08.S1.CO.")

plot(mod1, which = 1)

mod2 <- lm(data = air_norm, C6H6.GT. ~ PT08.S2.NMHC.)
summary(mod2)
## 
## Call:
## lm(formula = C6H6.GT. ~ PT08.S2.NMHC., data = air_norm)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.018385 -0.015140 -0.007871  0.007973  0.287650 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   -0.0856861  0.0006204  -138.1   <2e-16 ***
## PT08.S2.NMHC.  0.7980363  0.0018053   442.1   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.02177 on 6939 degrees of freedom
## Multiple R-squared:  0.9657, Adjusted R-squared:  0.9657 
## F-statistic: 1.954e+05 on 1 and 6939 DF,  p-value: < 2.2e-16
augment(mod2)%>%
   ggplot(aes(x = PT08.S2.NMHC., y = C6H6.GT.))+
  geom_point()+
  geom_line(aes(y= .fitted), color = "blue", size = 1)

residuals(mod2) %>% hist(main = "Residuals PT08.S2.NMHC.")

plot(mod2, which = 1)

mod3 <- lm(data = air_norm, C6H6.GT. ~ NOx.GT.)
summary(mod3)
## 
## Call:
## lm(formula = C6H6.GT. ~ NOx.GT., data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.20002 -0.06256 -0.01753  0.04394  0.61415 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.062395   0.001528   40.84   <2e-16 ***
## NOx.GT.     0.597921   0.006951   86.01   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.08179 on 6939 degrees of freedom
## Multiple R-squared:  0.516,  Adjusted R-squared:  0.5159 
## F-statistic:  7398 on 1 and 6939 DF,  p-value: < 2.2e-16
augment(mod3)%>%
   ggplot(aes(x = NOx.GT., y = C6H6.GT.))+
  geom_point()+
  geom_line(aes(y= .fitted), color = "blue", size = 1)

residuals(mod3) %>% hist(main = "Residuals NOx.GT.")

plot(mod3, which = 1)

mod4 <- lm(data = air_norm, C6H6.GT. ~ PT08.S3.NOx.)
summary(mod4)
## 
## Call:
## lm(formula = C6H6.GT. ~ PT08.S3.NOx., data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.14285 -0.05823 -0.01275  0.03632  0.69946 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   0.330684   0.002140  154.49   <2e-16 ***
## PT08.S3.NOx. -0.799672   0.009101  -87.87   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.08089 on 6939 degrees of freedom
## Multiple R-squared:  0.5267, Adjusted R-squared:  0.5266 
## F-statistic:  7721 on 1 and 6939 DF,  p-value: < 2.2e-16
augment(mod4)%>%
   ggplot(aes(x = PT08.S3.NOx., y = C6H6.GT.))+
  geom_point()+
  geom_line(aes(y= .fitted), color = "blue", size = 1)

residuals(mod4) %>% hist(main = "Residuals PT08.S3.NOx.")

plot(mod4, which = 1)

mod5 <- lm(data = air_norm, C6H6.GT. ~ NO2.GT.)
summary(mod5)
## 
## Call:
## lm(formula = C6H6.GT. ~ NO2.GT., data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.28123 -0.05625 -0.00966  0.04071  0.68139 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.004056   0.002881  -1.408    0.159    
## NO2.GT.      0.494451   0.007848  63.005   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09377 on 6939 degrees of freedom
## Multiple R-squared:  0.3639, Adjusted R-squared:  0.3638 
## F-statistic:  3970 on 1 and 6939 DF,  p-value: < 2.2e-16
augment(mod5)%>%
   ggplot(aes(x = NO2.GT., y = C6H6.GT.))+
  geom_point()+
  geom_line(aes(y= .fitted), color = "blue", size = 1)

residuals(mod5) %>% hist(main = "Residuals NO2.GT.")

plot(mod5, which = 1)

mod6 <- lm(data = air_norm, C6H6.GT. ~ PT08.S4.NO2.)
summary(mod6)
## 
## Call:
## lm(formula = C6H6.GT. ~ PT08.S4.NO2., data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.15656 -0.05635 -0.00724  0.04597  0.79223 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -0.065500   0.002506  -26.14   <2e-16 ***
## PT08.S4.NO2.  0.563771   0.005755   97.96   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.07616 on 6939 degrees of freedom
## Multiple R-squared:  0.5803, Adjusted R-squared:  0.5803 
## F-statistic:  9596 on 1 and 6939 DF,  p-value: < 2.2e-16
augment(mod6)%>%
   ggplot(aes(x = PT08.S4.NO2., y = C6H6.GT.))+
  geom_point()+
  geom_line(aes(y= .fitted), color = "blue", size = 1)

residuals(mod6) %>% hist(main = "Residuals PT08.S4.NO2.")

plot(mod6, which = 1)

mod7 <- lm(data = air_norm,C6H6.GT.  ~ PT08.S5.O3.)
summary(mod7)
## 
## Call:
## lm(formula = C6H6.GT. ~ PT08.S5.O3., data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.24981 -0.03674 -0.00059  0.03326  0.52482 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.045328   0.001642  -27.61   <2e-16 ***
## PT08.S5.O3.  0.573303   0.004063  141.11   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.05977 on 6939 degrees of freedom
## Multiple R-squared:  0.7416, Adjusted R-squared:  0.7415 
## F-statistic: 1.991e+04 on 1 and 6939 DF,  p-value: < 2.2e-16
augment(mod7)%>%
   ggplot(aes(x = PT08.S5.O3., y = C6H6.GT.))+
  geom_point()+
  geom_line(aes(y= .fitted), color = "blue", size = 1)

residuals(mod7) %>% hist(main = "Residuals PT08.S5.O3.")

plot(mod7, which = 1)

mod8 <- lm(data = air_norm,C6H6.GT. ~ `T`)
summary(mod8)
## 
## Call:
## lm(formula = C6H6.GT. ~ T, data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.17626 -0.08503 -0.02819  0.05791  0.86597 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.113686   0.003377   33.66   <2e-16 ***
## T           0.116814   0.007286   16.03   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1155 on 6939 degrees of freedom
## Multiple R-squared:  0.03572,    Adjusted R-squared:  0.03558 
## F-statistic: 257.1 on 1 and 6939 DF,  p-value: < 2.2e-16
augment(mod8)%>%
   ggplot(aes(x = `T`, y = C6H6.GT.))+
  geom_point()+
  geom_line(aes(y= .fitted), color = "blue", size = 1)

residuals(mod8) %>% hist(main = "Residuals T")

plot(mod8, which = 1)

mod9 <- lm(data = air_norm, C6H6.GT. ~ RH)
summary(mod9)
## 
## Call:
## lm(formula = C6H6.GT. ~ RH, data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.16472 -0.08857 -0.02776  0.06256  0.83736 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.168841   0.003508  48.131   <2e-16 ***
## RH          -0.011576   0.006434  -1.799   0.0721 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1175 on 6939 degrees of freedom
## Multiple R-squared:  0.0004662,  Adjusted R-squared:  0.0003221 
## F-statistic: 3.236 on 1 and 6939 DF,  p-value: 0.07206
augment(mod9)%>%
   ggplot(aes(x = RH, y = C6H6.GT.))+
  geom_point()+
  geom_line(aes(y= .fitted), color = "blue", size = 1)

residuals(mod9) %>% hist(main = "Residuals RH")

plot(mod9, which = 1)

mod10 <- lm(data = air_norm, C6H6.GT. ~ AH)
summary(mod10)
## 
## Call:
## lm(formula = C6H6.GT. ~ AH, data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.19607 -0.08744 -0.02681  0.06081  0.86382 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.119150   0.003096   38.49   <2e-16 ***
## AH          0.109438   0.006899   15.86   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1155 on 6939 degrees of freedom
## Multiple R-squared:  0.035,  Adjusted R-squared:  0.03486 
## F-statistic: 251.6 on 1 and 6939 DF,  p-value: < 2.2e-16
augment(mod10)%>%
   ggplot(aes(x = AH, y = C6H6.GT.))+
  geom_point()+
  geom_line(aes(y= .fitted), color = "blue", size = 1)

residuals(mod10) %>% hist(main = "Residuals AH")

plot(mod10, which = 1)

pairs(air_norm[,sapply(air_norm, is.double)])

Check correlation.

corrplot.mixed(cor(air_norm, method = "kendall"), number.cex = .7)

Check multicolinearity. I decided to see what’s going on by hands. For multic. we need to have CI>30 and VIF>10. Also, we can see it when coeficients of columns alone are not significant but together they give significant p-value.

# We can see multicolinearity here (a bit)
# But it is not good case - all these columns can't be used because of their non linearity and low correlation
mod <-  lm(C6H6.GT.~`T` + AH + RH, data = air_norm)
summary(mod)
## 
## Call:
## lm(formula = C6H6.GT. ~ T + AH + RH, data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.17485 -0.08416 -0.02811  0.05831  0.87498 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.04650    0.01159   4.013 6.07e-05 ***
## T            0.19929    0.02528   7.885 3.64e-15 ***
## AH          -0.03308    0.02008  -1.647   0.0996 .  
## RH           0.09135    0.01673   5.460 4.93e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1148 on 6937 degrees of freedom
## Multiple R-squared:  0.04669,    Adjusted R-squared:  0.04627 
## F-statistic: 113.2 on 3 and 6937 DF,  p-value: < 2.2e-16
X <- model.matrix(~`T` + AH + RH, data = air_norm)
XX <- t(X) %*% X
eigen <- eigen(XX)
CI <- sqrt(max(eigen$values) / min(eigen$values))
CI
## [1] 34.1049
# Also can count it via formula 1/1-R^2
ols_coll_diag(mod)
## Tolerance and Variance Inflation Factor
## ---------------------------------------
##   Variables  Tolerance       VIF
## 1         T 0.08216797 12.170192
## 2        AH 0.11659454  8.576731
## 3        RH 0.14110057  7.087144
## 
## 
## Eigenvalue and Condition Index
## ------------------------------
##    Eigenvalue Condition Index    intercept            T          AH          RH
## 1 3.627541457        1.000000 0.0010262758 0.0009364301 0.001661365 0.001468627
## 2 0.273793131        3.639946 0.0009294706 0.0190090965 0.004966602 0.045655253
## 3 0.094014653        6.211671 0.0617911881 0.0075634464 0.127247349 0.006326774
## 4 0.004650759       27.928284 0.9362530655 0.9724910270 0.866124684 0.946549345
residuals(mod) %>% hist(main = "Residuals T + AH + RH")

plot(mod, which = 1)

# No multicolinearity 
mod <-  lm(C6H6.GT.~CO.GT. + NOx.GT., data = air_norm)
summary(mod)
## 
## Call:
## lm(formula = C6H6.GT. ~ CO.GT. + NOx.GT., data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.24146 -0.02456 -0.00253  0.02323  0.77593 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.0052993  0.0009112   5.815 6.32e-09 ***
## CO.GT.       0.9211428  0.0068654 134.172  < 2e-16 ***
## NOx.GT.     -0.0285095  0.0059366  -4.802 1.60e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.04314 on 6938 degrees of freedom
## Multiple R-squared:  0.8654, Adjusted R-squared:  0.8653 
## F-statistic: 2.23e+04 on 2 and 6938 DF,  p-value: < 2.2e-16
X <- model.matrix(~CO.GT. + NOx.GT., data = air_norm)
XX <- t(X) %*% X
eigen <- eigen(XX)
CI <- sqrt(max(eigen$values) / min(eigen$values))
CI
## [1] 17.09533
ols_coll_diag(mod)
## Tolerance and Variance Inflation Factor
## ---------------------------------------
##   Variables Tolerance      VIF
## 1    CO.GT. 0.3814877 2.621317
## 2   NOx.GT. 0.3814877 2.621317
## 
## 
## Eigenvalue and Condition Index
## ------------------------------
##   Eigenvalue Condition Index  intercept     CO.GT.    NOx.GT.
## 1 2.67229931        1.000000 0.03758632 0.01625231 0.01989361
## 2 0.25191224        3.257001 0.84924286 0.03144661 0.17102841
## 3 0.07578845        5.938012 0.11317082 0.95230108 0.80907798
residuals(mod) %>% hist(main = "Residuals CO.GT. + NOx.GT.")

plot(mod, which = 1)

mod <-  lm(C6H6.GT.~NOx.GT. + NO2.GT. + AH, data = air_norm)
summary(mod)
## 
## Call:
## lm(formula = C6H6.GT. ~ NOx.GT. + NO2.GT. + AH, data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.24345 -0.04507 -0.01064  0.03143  0.64399 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.096338   0.003544  -27.18   <2e-16 ***
## NOx.GT.      0.419935   0.009245   45.42   <2e-16 ***
## NO2.GT.      0.291496   0.009616   30.31   <2e-16 ***
## AH           0.224735   0.004532   49.59   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.06968 on 6937 degrees of freedom
## Multiple R-squared:  0.6488, Adjusted R-squared:  0.6487 
## F-statistic:  4272 on 3 and 6937 DF,  p-value: < 2.2e-16
X <- model.matrix(~NOx.GT. + NO2.GT. + AH, data = air_norm)
XX <- t(X) %*% X
eigen <- eigen(XX)
CI <- sqrt(max(eigen$values) / min(eigen$values))
CI
## [1] 17.54622
ols_coll_diag(mod)
## Tolerance and Variance Inflation Factor
## ---------------------------------------
##   Variables Tolerance      VIF
## 1   NOx.GT. 0.4103626 2.436869
## 2   NO2.GT. 0.3678421 2.718558
## 3        AH 0.8437315 1.185211
## 
## 
## Eigenvalue and Condition Index
## ------------------------------
##   Eigenvalue Condition Index   intercept    NOx.GT.     NO2.GT.         AH
## 1 3.44660237        1.000000 0.004379087 0.01129197 0.004406544 0.01122907
## 2 0.40838783        2.905087 0.009069230 0.17704516 0.009599604 0.18104796
## 3 0.11748704        5.416274 0.108296306 0.39937511 0.109834562 0.38127768
## 4 0.02752276       11.190504 0.878255377 0.41228776 0.876159290 0.42644529
residuals(mod) %>% hist(main = "Residuals NOx.GT. + NO2.GT. + AH")

plot(mod, which = 1)

mod <-  lm(C6H6.GT.~NOx.GT. + NO2.GT., data = air_norm)
summary(mod)
## 
## Call:
## lm(formula = C6H6.GT. ~ NOx.GT. + NO2.GT., data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.23146 -0.06155 -0.01313  0.04352  0.61094 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.038596   0.002643   14.60   <2e-16 ***
## NOx.GT.     0.510196   0.010549   48.37   <2e-16 ***
## NO2.GT.     0.114112   0.010388   10.98   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.0811 on 6938 degrees of freedom
## Multiple R-squared:  0.5243, Adjusted R-squared:  0.5242 
## F-statistic:  3823 on 2 and 6938 DF,  p-value: < 2.2e-16
X <- model.matrix(~NOx.GT. + NO2.GT., data = air_norm)
XX <- t(X) %*% X
eigen <- eigen(XX)
CI <- sqrt(max(eigen$values) / min(eigen$values))
CI
## [1] 15.37171
ols_coll_diag(mod)
## Tolerance and Variance Inflation Factor
## ---------------------------------------
##   Variables Tolerance      VIF
## 1   NOx.GT. 0.4269069 2.342431
## 2   NO2.GT. 0.4269069 2.342431
## 
## 
## Eigenvalue and Condition Index
## ------------------------------
##   Eigenvalue Condition Index  intercept    NOx.GT.      NO2.GT.
## 1 2.72305554        1.000000 0.01617911 0.02059312 0.0085415824
## 2 0.23488007        3.404906 0.26014839 0.40997491 0.0009045351
## 3 0.04206439        8.045832 0.72367250 0.56943197 0.9905538825
residuals(mod) %>% hist(main = "Residuals NOx.GT. + NO2.GT.")

plot(mod, which = 1)

mod <-  lm(C6H6.GT.~CO.GT. + PT08.S1.CO. + PT08.S5.O3., data = air_norm)
summary(mod)
## 
## Call:
## lm(formula = C6H6.GT. ~ CO.GT. + PT08.S1.CO. + PT08.S5.O3., data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.19819 -0.02425  0.00054  0.02196  0.70684 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.027681   0.001254  -22.08   <2e-16 ***
## CO.GT.       0.625568   0.008562   73.06   <2e-16 ***
## PT08.S1.CO.  0.124724   0.007857   15.87   <2e-16 ***
## PT08.S5.O3.  0.104544   0.006442   16.23   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.03974 on 6937 degrees of freedom
## Multiple R-squared:  0.8858, Adjusted R-squared:  0.8857 
## F-statistic: 1.794e+04 on 3 and 6937 DF,  p-value: < 2.2e-16
X <- model.matrix(~CO.GT. + PT08.S1.CO. + PT08.S5.O3., data = air_norm)
XX <- t(X) %*% X
eigen <- eigen(XX)
CI <- sqrt(max(eigen$values) / min(eigen$values))
CI
## [1] 24.04993
ols_coll_diag(mod)
## Tolerance and Variance Inflation Factor
## ---------------------------------------
##     Variables Tolerance      VIF
## 1      CO.GT. 0.2080751 4.805957
## 2 PT08.S1.CO. 0.1494776 6.689966
## 3 PT08.S5.O3. 0.1758478 5.686737
## 
## 
## Eigenvalue and Condition Index
## ------------------------------
##   Eigenvalue Condition Index   intercept      CO.GT. PT08.S1.CO. PT08.S5.O3.
## 1 3.76111422        1.000000 0.008939149 0.004412538 0.001831225 0.002319881
## 2 0.18582994        4.498839 0.514072177 0.114246563 0.001347561 0.002769346
## 3 0.03495568       10.372880 0.412054216 0.806626579 0.068734735 0.375319140
## 4 0.01810016       14.415080 0.064934458 0.074714320 0.928086479 0.619591633
residuals(mod) %>% hist(main = "Residuals CO.GT. + PT08.S1.CO. + PT08.S5.O3.")

plot(mod, which = 1)

Final model Residuals should be normally distributed and the Q-Q Plot will show this. If residuals follow close to a straight line on this plot, it is a good indication they are normally distributed.

# Good model but NOx.GT. not normally distributed
# I've decided not to transform data, because I don't want to see log-log transformation
mod <-  lm(C6H6.GT.~NOx.GT. + NO2.GT. + PT08.S4.NO2., data = air_norm)
summary(mod)
## 
## Call:
## lm(formula = C6H6.GT. ~ NOx.GT. + NO2.GT. + PT08.S4.NO2., data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.13793 -0.02334 -0.00333  0.01923  0.61854 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -0.139891   0.001599  -87.50   <2e-16 ***
## NOx.GT.       0.354376   0.004819   73.54   <2e-16 ***
## NO2.GT.       0.156031   0.004661   33.47   <2e-16 ***
## PT08.S4.NO2.  0.470018   0.002828  166.19   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.03634 on 6937 degrees of freedom
## Multiple R-squared:  0.9045, Adjusted R-squared:  0.9045 
## F-statistic: 2.19e+04 on 3 and 6937 DF,  p-value: < 2.2e-16
X <- model.matrix(~NOx.GT. + NO2.GT. + PT08.S4.NO2., data = air_norm)
XX <- t(X) %*% X
eigen <- eigen(XX)
CI <- sqrt(max(eigen$values) / min(eigen$values))
CI
## [1] 16.71186
ols_coll_diag(mod)
## Tolerance and Variance Inflation Factor
## ---------------------------------------
##      Variables Tolerance      VIF
## 1      NOx.GT. 0.4107447 2.434602
## 2      NO2.GT. 0.4256569 2.349310
## 3 PT08.S4.NO2. 0.9425730 1.060926
## 
## 
## Eigenvalue and Condition Index
## ------------------------------
##   Eigenvalue Condition Index   intercept    NOx.GT.     NO2.GT. PT08.S4.NO2.
## 1 3.58202976        1.000000 0.005316617 0.01080633 0.004824685  0.008748795
## 2 0.29223051        3.501078 0.047103805 0.34153649 0.004969351  0.087717786
## 3 0.08986110        6.313624 0.140953828 0.14626091 0.197960256  0.667861110
## 4 0.03587862        9.991869 0.806625751 0.50139628 0.792245709  0.235672308
ols_plot_resid_fit_spread(mod)

ols_plot_obs_fit(mod)

mod <-  lm(C6H6.GT.~CO.GT. + PT08.S4.NO2., data = air_norm)
summary(mod)
## 
## Call:
## lm(formula = C6H6.GT. ~ CO.GT. + PT08.S4.NO2., data = air_norm)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.16444 -0.01879 -0.00216  0.01556  0.76399 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -0.051025   0.001126  -45.33   <2e-16 ***
## CO.GT.        0.718581   0.004320  166.32   <2e-16 ***
## PT08.S4.NO2.  0.215265   0.003322   64.81   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.03411 on 6938 degrees of freedom
## Multiple R-squared:  0.9159, Adjusted R-squared:  0.9158 
## F-statistic: 3.776e+04 on 2 and 6938 DF,  p-value: < 2.2e-16
X <- model.matrix(~CO.GT. + PT08.S4.NO2., data = air_norm)
XX <- t(X) %*% X
eigen <- eigen(XX)
CI <- sqrt(max(eigen$values) / min(eigen$values))
CI
## [1] 13.33846
ols_coll_diag(mod)
## Tolerance and Variance Inflation Factor
## ---------------------------------------
##      Variables Tolerance      VIF
## 1       CO.GT. 0.6020484 1.660996
## 2 PT08.S4.NO2. 0.6020484 1.660996
## 
## 
## Eigenvalue and Condition Index
## ------------------------------
##   Eigenvalue Condition Index  intercept     CO.GT. PT08.S4.NO2.
## 1 2.76742714        1.000000 0.01578747 0.02263258  0.010088266
## 2 0.18068900        3.913562 0.29140318 0.63351920  0.006558737
## 3 0.05188386        7.303347 0.69280934 0.34384822  0.983352997
ols_plot_resid_fit_spread(mod)

ols_plot_obs_fit(mod)

Separated data in 75:25% We can see that R^2 is close to 1 which says about linear dependence. P-value is close to 0 - independent variables explain the dynamics of the dependent variable.

set.seed(2)
sep <- sample.int(n = nrow(air_norm), size = floor(.75*nrow(air_norm)))
train <- air_norm[sep,]
test <- air_norm[-sep,]
train1 <- train[,c('C6H6.GT.', 'CO.GT.', 'PT08.S4.NO2.')]
test1 <- test[,c('C6H6.GT.', 'CO.GT.', 'PT08.S4.NO2.')]
mod1 <- lm(C6H6.GT.~CO.GT. + PT08.S4.NO2., data = train1)
a1 <- summary(mod1)
pred1 <- predict(mod1, newdata = test1)
test1$C6H6.GT._pred <- pred1
head(test1)
test1 <- rbindlist(list(test1[,c(2,3,1)], test1[,c(2,3,4)]))
train1$type <- 'train'
test1$type <- 'test'
test1[1:(nrow(test1)/2),4] <- 'real value'
all <- rbind(train1, test1)
plot_ly(data = all,
        z = ~C6H6.GT.,
        y = ~PT08.S4.NO2.,
        x = ~CO.GT., opacity = 0.7, color = ~type)%>%
  layout(title = paste("R2", round(a1$r.squared, 3),
                                  sep = ": "),
                            paste("pvalue", 2.2e-16, sep = ": "),
         xaxis = list(title = "CO.GT.",
                      zeroline = FALSE),
         yaxis = list(title = "PT08.S4.NO2.",
                      zeroline = FALSE),
         zaxis = list(title = "C6H6.GT.",
                      zeroline = FALSE))